#include "meta_obj.h"
#include "class.h"
#include "vm.h"
#include "common.h"

/**
 * @brief 新建模块
*/
ObjModule* new_objModule(VM *vm, const char *mod_name)
{
    ObjModule *obj_module = ALLOCATE(vm, ObjModule);
    if (obj_module == NULL)
    {
        MEM_ERROR("allocate ObjModule failed!");
    }

    // ObjModule是元信息对象，不属于任何一个类
    init_objheader(vm, &obj_module->obj_header, OT_MODULE, NULL);
    
    String_buffer_init(&obj_module->module_var_name);
    String_buffer_init(&obj_module->module_var_value);

    obj_module->name = NULL; // 核心模块名为NULL
    if (mod_name != NULL)
    {
        obj_module->name = new_objString(vm, mod_name, strlen(mod_name));
    }

    return obj_module;
}

/**
 * @brief 创建类class的实例
*/
ObjInstance* new_objInstance(VM *vm, Class *my_class)
{
    ObjInstance *obj_instance = ALLOCATE_EXTRA(vm, ObjInstance, sizeof(Value) * my_class->field_num);

    init_objheader(vm, &obj_instance->obj_header, OT_INSTANCE, my_class);
    // 初始化field为NULL
    uint32_t idx = 0;
    while (idx < my_class->field_num)
    {
        obj_instance->fields[idx ++] = VT_TO_VALUE(VT_NULL);
    }
    return obj_instance;
}